#!/bin/bash
process_name="mes-device"
SERVER_PORT="18081"
nacos_server_addr=""
nacos_config_namespace=""
nacos_config_name="mes-device-config"
app_log_home="/data/logs/mes-device"
nacos_discovery_namespace="public"
JAR_PATH="/data/mes/device/device-main.jar"
JVM_OPTS=""
env=""
if [ "$1" == 'dev' ]; then
    # shellcheck disable=SC2034
    env="dev"
    nacos_server_addr="120.77.81.170:8848"
    nacos_config_namespace="30fa26aa-ec7e-4e11-8979-35cac7f1eae1"
    JVM_OPTS="-Xmx512M -Xms512M -Xmn256M -XX:+UseParNewGC  -XX:+UseConcMarkSweepGC"

elif [ "$1" == 'test' ]; then
    # shellcheck disable=SC2034
    env="test"
    nacos_server_addr="120.77.81.170:8848"
    nacos_config_namespace="4673aaca-7c98-4c9d-a821-a2a26bd25ddd"
    nacos_discovery_namespace="4673aaca-7c98-4c9d-a821-a2a26bd25ddd"
    JVM_OPTS="-Xmx512M -Xms512M -Xmn256M -XX:+UseParNewGC  -XX:+UseConcMarkSweepGC"

elif [ "$1" == 'online' ]; then
    # shellcheck disable=SC2034
     env="online"
     nacos_server_addr="120.77.81.170:8848"
     nacos_config_namespace="d6146751-06a4-4f8f-ba0f-dc92968a9dea"
     nacos_discovery_namespace="d6146751-06a4-4f8f-ba0f-dc92968a9dea"
     JVM_OPTS="-Xmx2048M -Xms2048M -Xmn800M -XX:+UseParNewGC  -XX:+UseConcMarkSweepGC"
else
    echo "add env ........."
    exit
fi
pid=$(ps -ef | grep "$process_name" | grep -v grep | awk '{print $2}')
SERVER_IP=$(ip addr show | grep -oP '(?<=inet\s)\d+(\.\d+){3}' | grep -v '127.0.0.1')
echo "$process_name is running on $SERVER_IP , pid is $pid"
SERVER_DOWN_STATUS_IMG="http://auth-test.qaml.com.cn/static/1.png"
generate_server_down_post_data()
{
cat <<EOF
{
    "msgtype": "markdown",
    "markdown": {
        "title": "服务变更通知",
        "text": "### **服务名称**: $process_name \n ### **运行节点**: $SERVER_IP \n  ### **服务端口**: $SERVER_PORT \n ### **下线时间**: $SERVER_DOWN_DATE \n  ![]($SERVER_DOWN_STATUS_IMG)"
    }
}
EOF
}
if [[ -n $pid ]]; then
    SERVER_DOWN_DATE=$(date '+%Y-%m-%d %H:%M:%S')
    echo "kill process $process_name $pid start ......"
    curl -X DELETE "http://${SERVER_IP}:$SERVER_PORT/device/actuator/deregister/service" -w '\n'
    if [  "$1" == 'online' ]; then
         curl -X POST "https://oapi.dingtalk.com/robot/send?access_token=5a45aec38d2c28e68782dab509f64e88ffaa7bd039de9237c91c22ef6b54d74f" \
                         -H "Content-Type: application/json" \
                         -d "$(generate_server_down_post_data)" -w '\n'
    fi
    echo "deregister service end......"
    sleep 25s
    kill $pid
    echo "kill process $process_name $pid end ......"
    echo "start process $process_name ......"
    nohup java -Dspring.cloud.nacos.server-addr=$nacos_server_addr -Dspring.cloud.nacos.username=nacos -Dspring.cloud.nacos.password=nacos  -Dspring.cloud.nacos.discovery.namespace=$nacos_discovery_namespace -Dspring.cloud.nacos.config.namespace=$nacos_config_namespace -Dspring.cloud.nacos.config.name=$nacos_config_name -Dspring.cloud.nacos.config.file-extension=properties  -DLOGS_FOLDER=$app_log_home -DAPP_LOG_HOME=$app_log_home -Dserver.tomcat.accesslog.directory=$app_log_home -verbose:gc -Xloggc:$app_log_home/gc_start_at_$(date '+%Y%m%d%H%M%S').log -XX:ErrorFile=$app_log_home/java_error.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$app_log_home  $JVM_OPTS -server -jar $JAR_PATH >/dev/null 2>&1 &
    echo "start process $process_name end......"
else
    echo "process $process_name not found ......"
    echo "start process $process_name ......"
    nohup java -Dspring.cloud.nacos.server-addr=$nacos_server_addr -Dspring.cloud.nacos.username=nacos -Dspring.cloud.nacos.password=nacos  -Dspring.cloud.nacos.discovery.namespace=$nacos_discovery_namespace -Dspring.cloud.nacos.config.namespace=$nacos_config_namespace -Dspring.cloud.nacos.config.name=$nacos_config_name -Dspring.cloud.nacos.config.file-extension=properties  -DLOGS_FOLDER=$app_log_home -DAPP_LOG_HOME=$app_log_home -Dserver.tomcat.accesslog.directory=$app_log_home -verbose:gc -Xloggc:$app_log_home/gc_start_at_$(date '+%Y%m%d%H%M%S').log -XX:ErrorFile=$app_log_home/java_error.log -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=$app_log_home  $JVM_OPTS -server -jar $JAR_PATH >/dev/null 2>&1 &
    echo "start process $process_name end......"
fi

SERVER_UP_DATE=$(date '+%Y-%m-%d %H:%M:%S')
SERVER_UP_STATUS="http://auth-test.qaml.com.cn/static/0.png"
generate_server_up_post_data()
{
cat <<EOF
{
    "msgtype": "markdown",
    "markdown": {
        "title": "服务变更通知",
        "text": "### **服务名称**: $process_name \n ### **运行节点**: $SERVER_IP \n  ### **服务端口**: $SERVER_PORT \n ### **服务状态**: $status \n ### **上线时间**: $SERVER_UP_DATE \n  ![]($SERVER_UP_STATUS)"
    }
}
EOF
}
if [ "$1" == 'online' ]; then
    for ((i = 0; i < 3; i++)); do
            response=$(curl -s http://${SERVER_IP}:$SERVER_PORT/device/actuator/health)jp
            echo $response
            code=$(echo $response | jq -r '.code')
            data=$(echo $response | jq -r '.data')
            status=$(echo $data | jq -r '.status')
            if [[ $code == 200 && $status == "UP" ]]; then
                pid=$(ps -ef | grep "$process_name" | grep -v grep | awk '{print $2}')
                echo "process $process_name is running on $SERVER_IP , pid is $pid"
                curl -X POST "https://oapi.dingtalk.com/robot/send?access_token=5a45aec38d2c28e68782dab509f64e88ffaa7bd039de9237c91c22ef6b54d74f" \
                     -H "Content-Type: application/json" \
                     -d "$(generate_server_up_post_data)" -w '\n'
                exit 0
            else
                sleep 10s
            fi
        done
fi